package com.bo.taobao;

import java.text.DecimalFormat;


import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.domain.ItemCat;
import com.taobao.api.domain.TaobaokeItem;
import com.taobao.api.request.ItemcatsGetRequest;
import com.taobao.api.request.TaobaokeItemsCouponGetRequest;
import com.taobao.api.request.TaobaokeItemsGetRequest;
import com.taobao.api.response.ItemcatsGetResponse;
import com.taobao.api.response.TaobaokeItemsCouponGetResponse;
import com.taobao.api.response.TaobaokeItemsGetResponse;

public class Test {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		TaobaoClient client=new DefaultTaobaoClient(ApiClassProp.CONNURL, ApiClassProp.APPKEY, ApiClassProp.APPSECRET);
//		ItemcatsGetRequest req=new ItemcatsGetRequest();
//		req.setFields("cid,parent_cid,name,is_parent");
//		req.setParentCid(0L);
////		req.setCids("18957,19562,");
//		ItemcatsGetResponse response = client.execute(req);
//		response.getItemCats();
//		
//		
//		TmallSelectedItemsSearchRequest req2=new TmallSelectedItemsSearchRequest();
//		
//		for(ItemCat ic:response.getItemCats()){
//			System.out.println(ic.getCid()+"    "+ic.getName());
//		}
//			req2.setCid(ic.getCid());
//			TmallSelectedItemsSearchResponse response2 = client.execute(req2);
//			if(response2.getItemList()!=null){
//				for(SelectedItem si:response2.getItemList()){
//					System.out.println(si.getTrackIid());
//					
//					TaobaokeWidgetItemsConvertRequest req3=new TaobaokeWidgetItemsConvertRequest();
//					req3.setTrackIids(si.getTrackIid());
//					req3.setFields("num_iid,click_url,iid,commission,commission_rate,commission_num,commission_volume");
////					req3.setOuterCode("bbs");
////					req3.setNumIids("1,2,3");
////					req3.setIsMobile(true);
//					TaobaokeWidgetItemsConvertResponse response3 = client.execute(req3);
//					for(TaobaokeItem tt:response3.getTaobaokeItems()){
//						System.out.println(tt.getTitle());
//						System.out.println("商品价格"+tt.getPrice());
//						System.out.println("淘宝客佣金比率"+tt.getCommissionRate());
//						System.out.println("淘宝客佣金"+tt.getCommission());
//						System.out.println("30天内累计推广量"+tt.getCommissionNum());
//						System.out.println("累计总支出佣金量"+tt.getCommissionVolume());
//						System.out.println("30天内交易量"+tt.getVolume());
//						System.out.println("店铺类型:B(商城),C(集市)"+tt.getShopType());
//						System.out.println("折扣比率"+tt.getCouponRate());
//						System.out.println("折扣价格"+tt.getCouponPrice());
//						System.out.println("折扣活动开始时间"+tt.getCouponStartTime());
//						System.out.println("折扣活动结束时间"+tt.getCouponEndTime());
//						System.out.println("淘宝客类目推广URL"+tt.getTaobaokeCatClickUrl());
//						System.out.println("图片url"+tt.getPicUrl());
////						System.out.println("图片url"+tt.getPicUrl());
//					}
//				}
//			}
//			
//			
//		}

		
//		Test.test2(client);
		
		Test.weibo(client);
		
//		Test.test3(client);
		
		
		
	}
	
	/**
	 * 查询淘宝客推广商品
	 * @param client
	 * @throws ApiException 
	 */
	public static void test3(TaobaoClient client) throws ApiException{
		ItemcatsGetRequest req=new ItemcatsGetRequest();
		req.setFields("cid,parent_cid,name,is_parent");
		req.setParentCid(0L);
//		req.setCids("18957,19562,");
		ItemcatsGetResponse response = client.execute(req);
		response.getItemCats();
		
		
		for(ItemCat ic:response.getItemCats()){
			System.out.println(ic.getCid()+"    "+ic.getName());
		}
	}
	
	/**
	 * 查询淘宝客推广商品
	 * @param client
	 * @throws ApiException 
	 */
	public static void test1(TaobaoClient client) throws ApiException{
		TaobaokeItemsGetRequest req=new TaobaokeItemsGetRequest();
		req.setFields("num_iid,title,nick,pic_url,price,click_url,commission,commission_rate,commission_num,commission_volume,shop_click_url,seller_credit_score,item_location,volume");
//		TaobaokeItemsGetRequest req=new TaobaokeItemsGetRequest();
//		req.setFields("click_url");
		req.setCid(50026316L);
		TaobaokeItemsGetResponse response = client.execute(req);
		System.out.println(response.getTaobaokeItems());
		for(TaobaokeItem tt:response.getTaobaokeItems()){
			System.out.println(tt.getTitle());
			System.out.println("商品价格"+tt.getPrice());
			System.out.println("淘宝客佣金比率"+tt.getCommissionRate());
			System.out.println("淘宝客佣金"+tt.getCommission());
			System.out.println("30天内累计推广量"+tt.getCommissionNum());
			System.out.println("累计总支出佣金量"+tt.getCommissionVolume());
			System.out.println("30天内交易量"+tt.getVolume());
			System.out.println("店铺类型:B(商城),C(集市)"+tt.getShopType());
			System.out.println("折扣比率"+tt.getCouponRate());
			System.out.println("折扣价格"+tt.getCouponPrice());
			System.out.println("折扣活动开始时间"+tt.getCouponStartTime());
			System.out.println("折扣活动结束时间"+tt.getCouponEndTime());
			System.out.println("淘宝客类目推广URL"+tt.getClickUrl());
			System.out.println("图片url"+tt.getPicUrl());
	//		System.out.println("图片url"+tt.getPicUrl());
		}
	}
	
	/**
	 * 查询淘客折扣商品
	 * @param client
	 * @throws ApiException 
	 */
	public static void test2(TaobaoClient client) throws ApiException{
		TaobaokeItemsCouponGetRequest req=new TaobaokeItemsCouponGetRequest();
		req.setCid(50026316L);
		req.setIsMobile(true);
		req.setFields("num_iid,title,nick,pic_url,price,click_url,commission,commission_rate,commission_num,commission_volume,shop_click_url,seller_credit_score,item_location,volume,coupon_price,coupon_rate,coupon_start_time,coupon_end_time,shop_type");
		
		TaobaokeItemsCouponGetResponse response = client.execute(req);
		for(TaobaokeItem tt:response.getTaobaokeItems()){
			System.out.println(tt.getTitle());
			System.out.println("商品价格"+tt.getPrice());
			System.out.println("折扣价格"+tt.getCouponPrice());
			System.out.println("折扣比率"+tt.getCouponRate());
			System.out.println("30天内交易量"+tt.getVolume());
			System.out.println("折扣活动开始时间"+tt.getCouponStartTime());
			System.out.println("折扣活动结束时间"+tt.getCouponEndTime());
			
			System.out.println("淘宝客佣金比率"+tt.getCommissionRate());
			System.out.println("淘宝客佣金"+tt.getCommission());
			
			
			System.out.println("30天内累计推广量"+tt.getCommissionNum());
			System.out.println("累计总支出佣金量"+tt.getCommissionVolume());
			
			System.out.println("店铺类型:B(商城),C(集市)"+tt.getShopType());
			
			System.out.println("淘宝推广URL "+tt.getClickUrl());
			System.out.println("图片url"+tt.getPicUrl());
	//		System.out.println("图片url"+tt.getPicUrl());
//			BareBonesBrowserLaunch.openURL(tt.getClickUrl());
		}
		
	}

	
	/**
	 * 查询淘客折扣商品
	 * @param client
	 * @throws Exception 
	 */
	public static void weibo(TaobaoClient client) throws Exception{
//		Oauth oauth = new Oauth();         
//		BareBonesBrowserLaunch.openURL(oauth.authorize("code","state","scope"));         
//		System.out.print("Hitenter when it's done.[Enter]:");         
//		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));         
//		String code = br.readLine();  
//		
//		Timeline tm = new Timeline();  
//		
////		
//		AccessToken accessToken = null;         
//		try {             
//			accessToken = oauth.getAccessTokenByCode(code);         
//			} catch (WeiboException e) {             
//				if (401 == e.getStatusCode()) {                 
//					Log.logInfo("Unableto get the access token.");             
//				} else {                 
//					e.printStackTrace();         
//				}         
//			} 
//			tm.setToken(accessToken.getAccessToken());
				
		
		
//		
		TaobaokeItemsCouponGetRequest req=new TaobaokeItemsCouponGetRequest();
		req.setCid(50002766L);
		req.setStartCouponRate(2800L);
		req.setEndCouponRate(0000L);
		req.setSort("volume_desc");
		req.setPageSize(100L);
		req.setFields("num_iid,title,nick,pic_url,price,click_url,commission,commission_rate,commission_num,commission_volume,shop_click_url,seller_credit_score,item_location,volume,coupon_price,coupon_rate,coupon_start_time,coupon_end_time,shop_type");
		
		TaobaokeItemsCouponGetResponse response = client.execute(req);
		for(int i=0;i<20;i++){
			TaobaokeItem tt=response.getTaobaokeItems().get(i);
			StringBuffer sb=new StringBuffer();
			sb.append(tt.getTitle());
			sb.append("\n");
			sb.append("【");
			sb.append("市场价");
			sb.append(tt.getPrice());
			sb.append("元,折扣");
			String temp=tt.getCouponRate();
			temp=temp.substring(0, temp.indexOf("."));
			int n=Integer.parseInt(temp);
			double d=n*0.001;
//			sb.append(d);sb.append("    ");
			sb.append(new DecimalFormat("0.0").format(d));  
			sb.append("折,促销价");
			sb.append(tt.getCouponPrice());
			sb.append("元】");
			sb.append("\n");;
			sb.append("【");
			sb.append("30天内交易量");
			sb.append(tt.getVolume());
			sb.append("件");
			if(tt.getShopType().equals("B")){
				sb.append(";来源：天猫】");
			}else{
				sb.append("】");
			}
			sb.append("\n");;
			sb.append(tt.getClickUrl());;
			System.out.println("淘宝客佣金比率"+tt.getCommissionRate());
			System.out.println("淘宝客佣金"+tt.getCommission());
			System.out.println(sb.toString());
			
//			try {
//				Thread.sleep(30000);
//			} catch (InterruptedException e) {
//				e.printStackTrace();
//			}
//			
//			try {
//				byte[] content = getImageFromURL(tt.getPicUrl());
//				ImageItem pic = new ImageItem("pic", content);
//				String s = java.net.URLEncoder.encode(sb.toString(), "utf-8");
//				tm.UploadStatus(s, pic);
//			} catch (Exception e1) {
//				e1.printStackTrace();
//			}
		}
	}
	

	
	
	
}
